package interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import entity.Users;


public class LoginInterceptor implements HandlerInterceptor {
	private static final String[] IGNORE_URI = {"/jsp/login.jsp", "/login/"};
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {

	}
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {	
		boolean flag = false;
        String url = request.getRequestURL().toString();
        System.out.println(">>>: " + url);
        for (String s : IGNORE_URI) {
            if (url.contains(s)) {
                flag = true;
                break;
            }
        }
        Users username =  (Users)request.getSession().getAttribute("user"); 
        if (!flag) {        	  
            if (username != null){
            	flag = true;
            }else if(username == null){
            	flag = false;
            	response.sendRedirect("/OA/jsp/login.jsp"); 
            } 
        }
        return flag;
	}
}
	
	

